//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.sending;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.*;
import org.jpgolay.gui.fixed.SearchBox;
import org.jpgolay.gui.fixed.TableViewer;
import org.jpgolay.me.list.ListPrint;
import org.jpgolay.me.persistence.SQLList;
import org.jpgolay.me.util.*;

// Referenced classes of package org.jpgolay.me.sending:
//            SendingListPanel
public class SendingListController {

    public SendingListController(SendingListPanel sendingListPanel, Parameter param, JDesktopPane desktop) {
        this.sendingListPanel = sendingListPanel;
        this.param = param;
        this.desktop = desktop;
        sqlList = new SQLList();
        date = new MEDate();
        initSendingListPanel();
    }

    private void initSendingListPanel() {
        sendingListPanel.getSearch().setVal("");
        sendingListPanel.getSearch().setList(new String[]{
                    "N\260 BL", "Transporteur", "Assurance", "CODE CLIENT", "GLN CLIENT", "CODE AGENCE", "DESADV", "PREPA", "EXPED", "SSCC",
                    "GTIN PALET", "GTIN CART"
                });
        sendingListPanel.getSearch().setSelectedIndex(0);
        sendingListPanel.getSearch().getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        sendingListPanel.getSearch().getTextField().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        sendingListPanel.getPrintLabel().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionPrint();
            }
        });
        sendingListPanel.getTableViewerPanel().setTypes(new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.math.BigDecimal.class, java.lang.String.class, java.lang.String.class, java.lang.String.class,
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.math.BigDecimal.class, java.math.BigDecimal.class, java.lang.String.class
                });
        sendingListPanel.getTableViewerPanel().setLastColumnNotEditable(0);
        sendingListPanel.getTableViewerPanel().setCellEditable(false);
        sendingListPanel.getTableViewerPanel().setAlternateColor(MEUtility.SENDING_LINE);
        sendingListPanel.getTableViewerPanel().setColumnNames(new String[]{
                    "N\260", "Agence", "N\260 BL", "Desadv", "Transp", "Assur", "Valeur", "Code Clt", "GLN Clt", "Prep",
                    "Exped", "Livrer", "SSCC", "GTIN plt", "GTIN Crt", "Nb colis", "Poids", "Mdf"
                });
        sendingListPanel.getTableViewerPanel().setPreferredColumnSize(new int[]{
                    20, 70, 50, 50, 50, 50, 50, 50, 50, 50,
                    50, 50, 50, 50, 50, 50, 50, 60
                });
        actionSearch();
    }

    private Object[][] getDataEmpty() {
        Object data[][] = new Object[3][18];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            if (i == 1) {
                data[i][1] = "AUCUNE";
                data[i][2] = "EXPEDITION";
            } else {
                data[i][1] = null;
                data[i][2] = null;
            }
            data[i][3] = null;
            data[i][4] = null;
            data[i][5] = null;
            data[i][6] = null;
            data[i][7] = null;
            data[i][8] = null;
            data[i][9] = null;
            data[i][10] = null;
            data[i][11] = null;
            data[i][12] = null;
            data[i][13] = null;
            data[i][14] = null;
            data[i][15] = null;
            data[i][16] = null;
            data[i][17] = null;
        }

        return data;
    }

    private void actionSearch() {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT id,agency_id,order_id,delivery_form,trucking_company,insurance_company,insurance_value,supplier_id,supplier_gln,preparation_date,sending_date,delivery_date,sscc,pallet_gtin,box_gtin,parcel_quantity,weight,update_time FROM sending");
        switch (sendingListPanel.getSearch().getSelectedIndex()) {
            case 0: // '\0'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY order_id");
                } else {
                    sql.append(" WHERE order_id LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY order_id");
                }
                break;

            case 1: // '\001'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY trucking_company");
                } else {
                    sql.append(" WHERE trucking_company LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY trucking_company");
                }
                break;

            case 2: // '\002'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY insurance_company");
                } else {
                    sql.append(" WHERE insurance_company LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY insurance_company");
                }
                break;

            case 3: // '\003'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY supplier_id");
                } else {
                    sql.append(" WHERE supplier_id LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY supplier_id");
                }
                break;

            case 4: // '\004'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY supplier_gln");
                } else {
                    sql.append(" WHERE supplier_gln LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY supplier_gln");
                }
                break;

            case 5: // '\005'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY agency_id");
                } else {
                    sql.append(" WHERE agency_id=");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append(" ORDER BY agency_id");
                }
                break;

            case 6: // '\006'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY delivery_form");
                } else {
                    sql.append(" WHERE delivery_form LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY delivery_form");
                }
                break;

            case 7: // '\007'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY preparation_date");
                } else {
                    sql.append(" WHERE preparation_date LIKE '");
                    sql.append(date.toMysql(sendingListPanel.getSearch().getVal()));
                    sql.append("%' ORDER BY preparation_date");
                }
                break;

            case 8: // '\b'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY sending_date");
                } else {
                    sql.append(" WHERE sending_date LIKE '");
                    sql.append(date.toMysql(sendingListPanel.getSearch().getVal()));
                    sql.append("%' ORDER BY sending_date");
                }
                break;

            case 9: // '\t'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY sscc");
                } else {
                    sql.append(" WHERE sscc LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY sscc");
                }
                break;

            case 10: // '\n'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY pallet_gtin");
                } else {
                    sql.append(" WHERE pallet_gtin LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY pallet_gtin");
                }
                break;

            case 11: // '\013'
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY box_gtin");
                } else {
                    sql.append(" WHERE box_gtin LIKE '");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY box_gtin");
                }
                break;

            default:
                if (sendingListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id=");
                    sql.append(sendingListPanel.getSearch().getVal());
                    sql.append(" ORDER BY id");
                }
                break;
        }
        Object data[][] = (Object[][]) null;
        if (sqlList.search(sql.toString())) {
            data = sqlList.getRowsData();
            for (int i = 0; i < data.length; i++) {
                data[i][6] = (new BigDecimal((String) data[i][6])).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][9] = date.toME((String) data[i][9]);
                data[i][10] = date.toME((String) data[i][10]);
                data[i][11] = date.toME((String) data[i][11]);
                data[i][15] = (new BigDecimal((String) data[i][15])).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
                data[i][16] = (new BigDecimal((String) data[i][16])).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            }

        }
        if (data != null) {
            sendingListPanel.getTableViewerPanel().setData(data);
            sendingListPanel.getSearch().setVal("");
        } else {
            sendingListPanel.getTableViewerPanel().setData(getDataEmpty());
        }
    }

    private void actionPrint() {
        String columnName[] = {
            "col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8", "col9", "col10",
            "col11", "col12", "col13", "col14", "col15", "col16", "col17", "col18"
        };
        Object data[][] = sendingListPanel.getTableViewerPanel().getData();
        ListPrint listPrint = new ListPrint(data, columnName, "sendingList", true, param);
    }
    private SendingListPanel sendingListPanel;
    private Parameter param;
    private JDesktopPane desktop;
    private SQLList sqlList;
    private MEDate date;
}
